/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.v1.jitclouds.mda.cloud;

import br.ufba.lasid.v1.jitclouds.mda.api.JiTFTAssessmentAPI;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.BasicCluster;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.FTServiceLevel;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.ICluster;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.report.Report;
import br.ufba.lasid.v1.utils.ReadFileForAssessment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author viniciussantos
 *
 */
public class JiTFTAssessment implements JiTFTAssessmentAPI{


    @Override
    public Report evaluate(FTServiceLevel ftsLevel) throws Exception{
        Report r = new Report();
        Integer num_instances = ftsLevel.get("qtd_instances");
        String type = ftsLevel.get("vm_type");
        String MDA_HOME = System.getenv("MDA_HOME");           
        Runtime.getRuntime().exec(MDA_HOME + "att-azones.sh");
        List<BasicCluster> clusterList = new ArrayList<BasicCluster>();
        
        ReadFileForAssessment availability = new ReadFileForAssessment(MDA_HOME + "azones.txt");
        
        for(BasicCluster cluster: availability.getClusterList()){
            for(int i=0;i< cluster.getResources().getQTD_VMTYPES();i++){
                 if((cluster.getResources().getVmTypes().get(i).getFree() >= num_instances) &&
                    (cluster.getResources().getVmTypes().get(i).getName().equals(type))){
                     clusterList.add(cluster);
                 }
            }
               
            
        }
        
        r.set("ClusterList", clusterList);
        
        return r;
    }
    
    
    
}
